package net.ojbk.jiaofu.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import net.ojbk.jiaofu.po.SyTask;
import net.ojbk.jiaofu.service.StudentService;
import net.ojbk.jiaofu.service.TaskService;
import net.ojbk.jiaofu.service.collectService;
import net.ojbk.jiaofu.util.ExcelTool;
import net.ojbk.jiaofu.util.StringTool;
import net.ojbk.util.FileTool;
import net.ojbk.util.WebPathTool;

/**
 * Servlet implementation class collectServlet
 */
@WebServlet("/collectServlet")
@MultipartConfig(
        maxFileSize = 100*1024*1024,
        maxRequestSize = 130*1024*1024
) 
public class collectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public collectServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 控制 是否正确访问
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		HttpSession session=request.getSession();
		// 前奏 ↑ ↑ ↑ ↑ ↑
		// 判断是否登录 ↓
		if (!(session != null && session.getAttribute("login") != null && (boolean)session.getAttribute("login"))) { // 如果未登录
			String message = "<a href=\"./"
					+ FileTool.getFileName(request.getHeader("Referer"))
					+ "\">&nbsp;&nbsp;&nbsp;Return</a>";
			out.write("not login. <br/>" + message);
			System.out.println(FileTool.getFileName(request.getHeader("Referer")));
			return ;
		}
		// 获取 客户端 基本信息
		String userId = (String)session.getAttribute("myId");
		
		// 获取 post 参数 来构造 业务逻辑的 参数
		String taskId = request.getParameter("taskId");
		int taskType = TaskService.getTaskType(taskId);
		
		String baseBasicInfo[]=StudentService.getBasicInfoName(taskId);
		String basicInfo[] = StudentService.getBasicInfo(userId, taskId);
		String extraInfoName[]=TaskService.getExtraInfoName(taskId);
		String extraInfo[]=new String[extraInfoName.length];
		for(int i=0;i<extraInfoName.length;i++) {
			extraInfo[i]=request.getParameter(extraInfoName[i]);
		}
		
		switch (taskType) {
		case 1: case 2: // 截图\Zip
			// 接收文件
			String uploadPath =TaskService.getTreasuryPath(userId, taskId);
			System.out.println(uploadPath);
			File tempFile = new File(uploadPath);
			if(!tempFile.exists()) {
				tempFile.setWritable(true,false);
			}
			File uploadPathFile =tempFile;
			if(!uploadPathFile.exists()) {
				uploadPathFile.mkdirs();
			}
			
			SyTask task = TaskService.getTask(taskId);
			String basicInfoName[] = StudentService.getBasicInfoName(taskId);
			basicInfo= StudentService.getBasicInfo(userId, taskId);
/*			for(int i=0;i<basicInfo.length;i++) {
				System.out.println(basicInfo[i]);
			}*/
			String formatFileName = StringTool.formatFileName(task.getFileFormat(), basicInfoName, basicInfo, new String[0], new String[0]);
			
			
			
			
			Part part1 = request.getPart("file");
			
			File file1 = new File(uploadPathFile.getPath() + File.separator + formatFileName + FileTool.getFileType(part1.getSubmittedFileName()));
	        // file1 为文件
	        part1.write(file1.getPath()); // 写出
	        collectService.collectTask(userId, taskId, taskType, baseBasicInfo, extraInfo);
	        request.getRequestDispatcher("home.jsp").forward(request, response);
			break;
		case 3: // Info
			collectService.collectTask(userId, taskId, taskType,basicInfo, extraInfo);
			request.getRequestDispatcher("home.jsp").forward(request, response);
			break;
		}
		
		

		// 执行业务逻辑
		
	    
		// 重定向访问
		
		
	}

}
